package ldh.im.fxbase.dao;

import ldh.im.fxbase.data.pojo.ImModuleItem;
import ldh.im.fxbase.data.pojo.ext.ImModuleItemExt;
import ldh.im.fxbase.util.DbUtil;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class ImModuleItemDao extends BaseDao {

    private static final String MODULE_COLUMNS = "id, moduleId, name, description, icon, url, params, no, type typeValue, status statusValue, createTime, updateTime";

    public void insert(ImModuleItem imModuleItem) throws SQLException {
        DbUtil.transaction((queryRunner, connection) -> {
            ResultSetHandler<Long> resultSetHandler = new ScalarHandler();
            String sql = String.format("insert into im_module_item(moduleId, name, description, icon, url, params, no, type, status, createTime, updateTime) " +
                    "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            Long id = queryRunner.insert(connection, sql, resultSetHandler, imModuleItem.getModuleId(), imModuleItem.getName(), imModuleItem.getDescription(), imModuleItem.getIcon(),
                    imModuleItem.getUrl(), imModuleItem.getParams(), imModuleItem.getNo(), valueEnum(imModuleItem.getType()),
                    valueEnum(imModuleItem.getStatus()), imModuleItem.getCreateTime(), imModuleItem.getUpdateTime());
            imModuleItem.setId(id);
            return imModuleItem;
        });
    }

    public void batchInserts(List<ImModuleItem> imModuleItems) throws SQLException {
        if (imModuleItems.size() < 1) return;
        Object[][] params = imModuleItems.stream().map(imModuleItem-> new Object[]{imModuleItem.getModuleId(), imModuleItem.getName(), imModuleItem.getDescription(), imModuleItem.getIcon(),
                imModuleItem.getUrl(), imModuleItem.getParams(), imModuleItem.getNo(), valueEnum(imModuleItem.getType()),
                valueEnum(imModuleItem.getStatus()), imModuleItem.getCreateTime(), imModuleItem.getUpdateTime()}).toArray(Object[][]::new);
        DbUtil.transaction((queryRunner, connection) -> {
            ColumnListHandler<Long> resultSetHandler = new ColumnListHandler();
            String sql = String.format("insert into im_module_item(moduleId, name, description, icon, url, params, no, type, status, createTime, updateTime) " +
                    "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            List<Long> ids = queryRunner.insertBatch(connection, sql, resultSetHandler, params);
            int idx = 0;
            for(Long id : ids) {
                imModuleItems.get(idx++).setId(id);
            }
            return ids.size();
        });
    }

    public void batchUpdates(List<ImModuleItem> imModuleItems) throws SQLException {
        if (imModuleItems.size() < 1) return;
        Object[][] params = imModuleItems.stream().map(imModuleItem-> new Object[]{imModuleItem.getDescription(), imModuleItem.getIcon(), imModuleItem.getUrl(),
                imModuleItem.getParams(), imModuleItem.getNo(), valueEnum(imModuleItem.getType()), valueEnum(imModuleItem.getStatus()),
                imModuleItem.getUpdateTime(), imModuleItem.getId()}).toArray(Object[][]::new);

        DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("update im_module_item set description = ?, icon = ?, url = ?, params = ?, no = ?, type = ?,  status =?, updateTime = ? where id = ? ");
            int[] ns = queryRunner.batch(connection, sql, params);
            if (ns.length != imModuleItems.size()) {
                throw new RuntimeException("更新数据失败");
            }
            return ns.length;
        });
    }

    public ImModuleItem getByModuleIdAndName(Long moduleId, String name) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_module_item where moduleId = ? and name = ? limit 1", MODULE_COLUMNS);
            ImModuleItemExt imModuleItemExt = queryRunner.query(connection, sql, new BeanHandler<>(ImModuleItemExt.class), moduleId, name);
            return imModuleItemExt;
        });
    }

    public List<ImModuleItem> getByModuleId(Long moduleId) throws SQLException {
        return DbUtil.transaction((queryRunner, connection) -> {
            String sql = String.format("select %s from im_module_item where moduleId = ? order by no, updateTime desc", MODULE_COLUMNS);
            List<ImModuleItem> imModuleItemExtList = queryRunner.query(connection, sql, new BeanListHandler<>(ImModuleItemExt.class), moduleId);
            return imModuleItemExtList;
        });
    }

}
